var ClientItemAuth = require('./ClientItemAuth.js');
var ClientItemUnAuth = require('./ClientItemUnAuth.js');

function ClientItemMgr() {
	this.authList = [];
	this.unAuthList = [];
}

ClientItemMgr.prototype.connection = function(socket) {
	var that = this;

	// create -> add listener -> add to list
	var unAuthItem = new ClientItemUnAuth(socket);
	unAuthItem.start();

	unAuthItem.on("auth_done", function(deviceUuid) {
		that.removeUnAuth(unAuthItem);
		//unAuthItem.finish();

		that.addToAuthList(deviceUuid, socket);
	});

	// remove from unauth list, finish it
	unAuthItem.on("auth_timeout", function() {
		that.removeUnAuth(unAuthItem);
		unAuthItem.finish();
	});

	this.unAuthList.push(new ClientItemUnAuth(socket));
}

ClientItemMgr.prototype.addToAuthList = function(deviceUuid, socket) {
	// console.log("addToAuthList");
	// remove and finish socket(same uuid)
	var old = this.removeByDeviceUuid(deviceUuid);
	if (old) {
		console.log('remove old');
		old.finish();
	}

	// add new socket to auth list
	var item = new ClientItemAuth(deviceUuid, socket);
	item.start();
	this.authList.push(item);
	console.log("after add, auth socket size=" + this.authList.length);
	var that = this;
	item.on("tick_timeout", function() {
		that.removeAuth(item);
		item.finish();
	});
	item.on("socket_invisible", function() {
		that.removeAuth(item);
		item.finish();
	});
}

// remove from unauth
ClientItemMgr.prototype.removeUnAuth = function(clientItemUnAuth) {
	// console.log("removeUnAuth");
	var index = -1;
	for (var i = 0; i < this.unAuthList.length; i++) {
		if (clientItemUnAuth === this.unAuthList[i]) {
			index = i;
			break;
		}
	}
	if (index >= 0) {
		this.unAuthList.splice(index, 1);
	}
}

// remove from auth list
ClientItemMgr.prototype.removeAuth = function(authItem) {
	var index = -1;
	for (var i = 0; i < this.authList.length; i++) {
		if (authItem === this.authList[i]) {
			index = i;
			break;
		}
	}
	if (index >= 0) {
		console.log("removeAuth");
		this.authList.splice(index, 1);
	}
}

// remove, not do anything on this ClientItemAuth object
ClientItemMgr.prototype.removeByDeviceUuid = function(deviceUuid) {
	var index = -1;
	for (var i = 0; i < this.authList.length; i++) {
		if (this.authList[i].deviceUuid === deviceUuid) {
			index = i;
			break;
		}
	}
	if (index >= 0) {
		console.log("remove uuid=" + deviceUuid);
	} else {
		console.log('removeByDeviceUuid,no found:' + deviceUuid);
	}
	return index >= 0 ? this.authList.splice(index, 1)[0] : null;
}

ClientItemMgr.prototype.sendMsg = function(devices, msg) {
	if (Array.isArray(devices)) {
		console.log("send msg to many,size=" + devices.length);
		for (var i = 0; i < devices.length; i++) {
			this.sendMsgSingle(devices[i], msg);
		}
	} else {
		console.log("send msg to single");
		this.sendMsgSingle(devices, msg);
	}
}

ClientItemMgr.prototype.sendMsgSingle = function(deviceUuid, msg) {

	var target = null;
	for (var i = 0; i < this.authList.length; i++) {
		if (this.authList[i].deviceUuid === deviceUuid) {
			target = this.authList[i];
			break;
		}
	}
	if (target) {
		target.sendMsg(msg);
	}
}

ClientItemMgr.prototype.getAllOnline = function() {
	var list = [];
	for (var i = 0; i < this.authList.length; i++) {
		list.push(this.authList[i].deviceUuid);
	}
	return list;
}

module.exports = ClientItemMgr;